import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.linear_model import LogisticRegression,LogisticRegressionCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import confusion_matrix,accuracy_score
from sklearn.feature_selection import VarianceThreshold


train = pd.read_csv("Data/train.csv")
test = pd.read_csv("Data/test.csv")


train.drop('subject',axis=1,inplace=True)
test.drop('subject',axis=1,inplace=True)


rem_cols2 = train.columns.tolist()


#确定目标
is_object_type_feature = train.dtypes==np.object

train.columns[is_object_type_feature]

train['Activity'].value_counts()

le = LabelEncoder()

for x in [train,test]:
    x['Activity'] = le.fit_transform(x['Activity'])

train['Activity'].value_counts().sort_index()

#关联
corr_val = train.corr()

corr_val_activity = corr_val['Activity'].abs()

corr_val_activity_sort = corr_val['Activity'].abs().sort_values(ascending=False)

corr_val_activity_sort[corr_val_activity_sort > 0.83]


#切割（根据目标种类进行shuff切割）
feature_cols = train.columns[:-1]

split_data = StratifiedShuffleSplit(n_splits=3,test_size=0.3,random_state=42)

train_idx,val_idx=next(split_data.split(train[feature_cols],train.Activity))

X_train = train.loc[train_idx,feature_cols]
Y_train=train.loc[train_idx,'Activity']

X_val = train.loc[val_idx,feature_cols]
Y_val = train.loc[val_idx,'Activity']


#预测
lr = LogisticRegression(random_state=0, max_iter=5000)#方法一
lr_l2 = LogisticRegressionCV(random_state=0, max_iter=5000)#方法二
rf = RandomForestClassifier()#方法三


lr_model = lr.fit(X_train,Y_train)
lr_l2_model = lr_l2.fit(X_train,Y_train)
rf_model = rf.fit(X_train,Y_train)

lr_model_predict = lr_model.predict(X_val)
lr_l2_model_predict = lr_l2_model.predict(X_val)
rf_model_predict = rf_model.predict(X_val)

three_model_predict_df = pd.DataFrame({'lr':lr_l2_model_predict,'lr_l2':lr_l2_model_predict,'rf':rf_model_predict})


#三个模型的信心指数
lr_model_proba = lr_model.predict_proba(X_val).max(axis=1)
lr_l2_model_propa = lr_l2_model.predict_proba(X_val).max(axis=1)
rf_model_propa = rf_model.predict_proba(X_val).max(axis=1)

three_model_proba_df = pd.DataFrame({'lr_model_proba':lr_model_proba,'lr_l2_model_proba':lr_l2_model_propa,
'rf_model_proba':rf_model_propa})

#三个模型的评量调整
accuracy_lr = accuracy_score(Y_val,lr_model_predict)
accuracy_lr_l2 = accuracy_score(Y_val,lr_l2_model_predict)
accuracy_rf = accuracy_score(Y_val,rf_model_predict)

three_model_report = pd.DataFrame(data={'Accuracy':[accuracy_lr,accuracy_lr_l2,accuracy_rf]},index=['lr','lr_l2','rf'])



